﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Linq;

namespace Accountant.Controls
{
  public partial class MonthsPeaker
  {
    public static readonly DependencyProperty StartProperty =
      DependencyProperty.Register("Start", typeof (DateTime), 
      typeof (MonthsPeaker), new PropertyMetadata(default(DateTime), PropertyChangedCallback));

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
//      ValueSource valueSource = DependencyPropertyHelper.GetValueSource(dependencyObject, StartProperty);
//      var monthsPeaker = ((MonthsPeaker) dependencyObject);
//      var val = e.NewValue;
    }

    public DateTime Start
    {
      get { return (DateTime) GetValue(StartProperty); }
      set { SetValue(StartProperty, value); }
    }

    public static readonly DependencyProperty StopProperty =
      DependencyProperty.Register("Stop", typeof (DateTime), 
      typeof (MonthsPeaker), new PropertyMetadata(default(DateTime)));

    public DateTime Stop
    {
      get { return (DateTime) GetValue(StopProperty); }
      set { SetValue(StopProperty, value); }
    }

    public static readonly DependencyProperty ProposedDateFormatProperty =
      DependencyProperty.Register("ProposedDateFormat", typeof (string), 
      typeof (MonthsPeaker), new PropertyMetadata(default(string)));

    public string ProposedDateFormat
    {
      get { return (string) GetValue(ProposedDateFormatProperty); }
      set { SetValue(ProposedDateFormatProperty, value); }
    }
    public MonthsPeaker()
    {
      InitializeComponent();
      
      Initialized += OnInitialized;
      Loaded += OnLoaded;
    }



    private void OnInitialized(object sender, EventArgs eventArgs)
    {
      
    }
    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
      _year.Content = DateTime.Now.Year;
      _bttns = new[] { _year }.Concat(_grid.Children.OfType<ToggleButton>()).ToList();
      _bttns[DateTime.Now.Month].IsChecked = true;
    }
    private List<ToggleButton> _bttns;

    private void OnChecked(object sender, RoutedEventArgs e)
    {
      foreach (var bttn in _bttns)
        if (bttn != e.OriginalSource)
          bttn.IsChecked = false;

      Update();
    }

    private void Update()
    {
      var year = (int) _year.Content;
      if (_year.IsChecked == true)
      {
        Start = new DateTime(year, 1, 1);
        Stop = new DateTime(year + 1, 1, 1);
        ProposedDateFormat = "M";
        return;
      }

      var idx = 0;
      for (; idx < 12; idx++)
        if (((ToggleButton)_grid.Children[idx]).IsChecked == true)
          break;

      if (idx != 12)
      {
        var month = idx + 1;
        Start = new DateTime(year, month, 1);
        Stop = month < 12
                 ? new DateTime(year, month + 1, 1)
                 : new DateTime(year + 1, 1, 1);
        ProposedDateFormat = "dd, ddd";
      }
    }

    private void OnPrev(object sender, RoutedEventArgs e)
    {
      var year = (int)_year.Content;
      _year.Content = year-1;
      Update();
    }

    private void OnNext(object sender, RoutedEventArgs e)
    {
      var year = (int)_year.Content;
      _year.Content = year + 1;
      Update();
    }
  }
}
